package com.tjetc.controller;


import com.tjetc.common.JsonResult;
import com.tjetc.entity.User;
import com.tjetc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;


@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /***
     * 添加用户
     * @param user
     * @return
     */
    @RequestMapping("/add")
    public JsonResult adduser(User user) {
        Integer id = userService.insert(user);
        JsonResult result = new JsonResult<>(0, "", id);
        return result;
    }

    /***
     * 删除用户
     * @param id
     * @return
     */
    @RequestMapping("/delete/{id}")
    public JsonResult delete(@PathVariable("id") Integer id) {
        int userid = userService.deleteById(id);
        if(userid == 0)return new JsonResult(0,"用户不存在",false);
        return new JsonResult(1,"用户删除成功",true);
    }

    /***
     * 更新用户信息
     * @param user
     * @return
     */
    @RequestMapping("/update")
    public JsonResult update(User user){
        int row = userService.update(user);
        JsonResult result = new JsonResult<>(0, "", row);
        return result;
    }

    /***
     * 用户id查询用户信息
     * @param id
     * @return
     */
    @RequestMapping("/findUser/{id}")
    public JsonResult findUserById(@PathVariable("id") Integer id){
        User user = userService.selectById(id);
        if(user!=null){
            return new JsonResult(0, "存在这样的用户", user);
        }else{
            return new JsonResult(0, "不存在这样的用户","");
        }

    }

    /***
     * 更新用户头像
     * @param id
     * @return
     */
    @RequestMapping("updateImg")
    public JsonResult updateImg(Integer id){
        return new JsonResult<>(0, "", userService.updateImg(id));
    }

    /***
     * 更改密码检验用户信息
     * @param username
     * @param password
     * @param phone
     * @return
     */
    @RequestMapping("check")
    public JsonResult check(String username,String password,String phone){
        return userService.check(username,password,phone);
    }

    /***
     * 手机号登录检验信息
     * @param phone
     * @return
     */
    @RequestMapping("loginbyphone")
    public JsonResult loginByPhone(@RequestParam("phone") String phone) {
        return userService.loginByPhone(phone);
    }

}
